package com.sanchunhui.psc.ui.fragment

import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eightbitlab.rxbus.Bus
import com.eightbitlab.rxbus.registerInBus
import com.facebook.drawee.backends.pipeline.Fresco
import com.kotlin.message.data.protocol.Message
import com.sanchunhui.psc.R
import com.sanchunhui.psc.event.NoticeMessageEvent
import com.sanchunhui.psc.event.TeamAllCheckedEvent
import com.sanchunhui.psc.ui.adapter.NoticeMessageAdapter
import com.sanchunhui.psc.utils.NoticeObservable
import kotlinx.android.synthetic.main.fragment_notice.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.find
import org.json.JSONObject
import java.util.*

/**
 * @Author zyx
 * @Date 2021/5/18-10:02
 * @Dscribe 公告消息
 */
class NoticeFragment : Fragment() {

    lateinit var noticeMessageAdapter : NoticeMessageAdapter
    lateinit var recyclerViewNotice : RecyclerView
    var mediaPlayer: MediaPlayer?=null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = View.inflate(activity, R.layout.fragment_notice, null)
        recyclerViewNotice = view.find<RecyclerView>(R.id.rv_notice)
        recyclerViewNotice.layoutManager = LinearLayoutManager(activity)
        noticeMessageAdapter = NoticeMessageAdapter(activity)
        recyclerViewNotice.adapter = noticeMessageAdapter
        mediaPlayer = MediaPlayer.create(activity, R.raw.arm_sound)
        Fresco.initialize(context)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initObserve()
    }

    val allList: ArrayList<Message> = ArrayList()

    private fun  initObserve() {
        Bus.observe<NoticeMessageEvent>().subscribe {
            run {
                //观察者的响应
                Log.e("alarms","观察者收到了消息内容:"+it)
                val jsonObj : JSONObject = JSONObject(it.extras)
//        val pushId = jsonObj.getString("orderId")
                val pushTitle = jsonObj.getString("msgTitle")
                val pushContent = jsonObj.getString("msgContent")

                val messageBean = Message("",pushTitle,pushContent,"")

                allList.add(messageBean!!)

                if (allList.size > 0) {
                    tv_notice_empty.visibility = View.GONE
                } else {
                    tv_notice_empty.visibility = View.VISIBLE
                }

                noticeMessageAdapter.setData(allList)

                //播放音乐
                mediaPlayer!!.start()
                mediaPlayer!!.prepareAsync()
            }
        }.registerInBus(this)
    }
//
//    init {
//        NoticeObservable.instance.addObserver(this)  //让观察者和被观察者建立绑定关系
//
//    }



//    override fun update(observer: Observable?, data: Any?) {
//
//        //观察者的响应
//        Log.e("alarms","观察者收到了消息内容:"+data)
//        val jsonObj : JSONObject = JSONObject(data as String)
////        val pushId = jsonObj.getString("orderId")
//        val pushTitle = jsonObj.getString("msgTitle")
//        val pushContent = jsonObj.getString("msgContent")
//
//        val messageBean = Message("",pushTitle,pushContent,"")
//
//        allList.add(messageBean!!)
//
//        if (allList.size > 0) {
//            tv_notice_empty.visibility = View.GONE
//        } else {
//            tv_notice_empty.visibility = View.VISIBLE
//        }
//
//        noticeMessageAdapter.setData(allList)
//
//        //播放音乐
//        mediaPlayer!!.start()
//        mediaPlayer!!.prepareAsync()
//
//    }
}